package com.supermap.rootstock.admin.mapper;

import cn.hutool.core.date.DateUtil;
import com.supermap.rootstock.admin.api.entity.Arithmetic;
import com.supermap.rootstock.admin.api.admin.arithmetic.ArithmeticParam;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description 算法中心 所有接口
 * @date 2019年09月12 11:33
 * @email 1301584033@qq.com
 */
@Mapper
public interface AritmeticMapper {

	@SelectProvider(type =AritmeticMapperSQL.class,method = "getPageArithmeticListSQL")
	List<Map<String,Object>>  getPageArithmeticList(@Param("name") String name,@Param("systemId") Integer systemId,@Param("langId")Integer  langId, @Param("pageNum")Integer pageNum,@Param("pageSize") Integer pageSize);


	@Update("update t_arithmetic set state=#{state},suserId=#{suserId},scontent=#{scontent} where id=1")
	Integer postAudit(@Param("state") Integer state,@Param("suserId") Integer suserId,@Param("scontent") String scontent);

	@SelectProvider(type =AritmeticMapperSQL.class,method = "getPageArithmeticListCountNumSQL")
	Integer  getPageArithmeticListCountNum(@Param("name") String name,@Param("systemId") Integer systemId,@Param("langId")Integer  langId);

	@Select("SELECT * FROM t_arithmetic WHERE id=#{aId}")
	Map<String,Object> getArithmeticById(@Param("aId")Integer aId);

	@Select("SELECT * FROM t_arithmetic_lang")
	List<Map<String,Object>> getArithmeticLangList();

	@Select("select last_insert_id()")
	Integer getId();

	@Select("SELECT * FROM t_arithmetic_sysytem")
	List<Map<String,Object>> getArithmeticSystemList();

	@InsertProvider(type = AritmeticMapperSQL.class,method = "putArithmeticSQL")
	void putArithmetic(@Param("arithmetic") Arithmetic arithmetic);

	@InsertProvider(type = AritmeticMapperSQL.class,method = "putArithmeticParmsSQL")
	void putArithmeticParms(@Param("arithmeticParams") List<ArithmeticParam> arithmeticParams);

	@Delete("delete from t_arithmetic_param where aritId=#{aId}")
	void delArithmeticParms(@Param("aId")Integer aId);

class AritmeticMapperSQL{

	public String putArithmeticParmsSQL(Map<String,Object> map){
		StringBuffer stringBuffer=new StringBuffer();
		List<ArithmeticParam> arithmeticParams=(List<ArithmeticParam>)map.get("arithmeticParams");

		stringBuffer.append("insert into t_arithmetic_param(aritId,cname,name,value,content) values ");
		for(ArithmeticParam param:arithmeticParams){
			stringBuffer.append("("+param.getAritId()+",'"+param.getCname()+"','"+param.getName()+"','"+param.getValue()+"','"+param.getConten()+"'),");
		}
		return stringBuffer.substring(0,stringBuffer.length()-1);
	}
	//添加
	public String putArithmeticSQL(Map<String,Object> map){
		StringBuffer stringBuffer=new StringBuffer();
		Arithmetic arithmetic=(Arithmetic)map.get("arithmetic");
		String date=DateUtil.now();
		StringBuilder title=new StringBuilder();
		StringBuilder val=new StringBuilder();
		stringBuffer.append(" insert into t_arithmetic ");
		if(Strings.isNotBlank(arithmetic.getName())){
			title.append("name,");
			val.append("'"+arithmetic.getName()+"',");
		}
		if(arithmetic.getSystemId()!=null){
			title.append("systemId,");
			val.append(""+arithmetic.getSystemId()+",");
		}
		if(arithmetic.getLangId()!=null){
			title.append("langId,");
			val.append(""+arithmetic.getLangId()+",");
		}
		if(arithmetic.getUserId()!=null){
			title.append("userId,");
			val.append(""+arithmetic.getUserId()+",");
		}
		if(Strings.isNotBlank(arithmetic.getDutyUnit())){
			title.append("dutyUnit,");
			val.append("'"+arithmetic.getDutyUnit()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getVersion())){
			title.append("version,");
			val.append("'"+arithmetic.getVersion()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getAtype())){
			title.append("atype,");
			val.append("'"+arithmetic.getAtype()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getAconcent())){
			title.append("aconcent,");
			val.append("'"+arithmetic.getAconcent()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getAOrder())){
			title.append("aOrder,");
			val.append("'"+arithmetic.getAOrder()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getFileServicePath())){
			title.append("fileServicePath,");
			val.append("'"+arithmetic.getFileServicePath()+"',");
		}
		if(Strings.isNotBlank(arithmetic.getDocServicePath())){
			title.append("docServicePath,");
			val.append("'"+arithmetic.getDocServicePath()+"',");
		}
		if(arithmetic.getState()!=null){
			title.append("state,");
			val.append(""+arithmetic.getState()+",");
			title.append("stime,");
			val.append("'"+date+"',");
		}
		if(arithmetic.getSuserId()!=null){
			title.append("suserId,");
			val.append(""+arithmetic.getSuserId()+",");
		}
		title.append("ctime,");
		val.append("'"+date+"',");
		stringBuffer.append("("+title.substring(0,title.lastIndexOf(","))+") values("+val.substring(0,val.lastIndexOf(","))+")");
		return  stringBuffer.toString();
	}


	//分页总数
	public String getPageArithmeticListCountNumSQL(Map<String,Object> map){
		StringBuffer stringBuffer=new StringBuffer();
		String name=(String)map.get("name");
		Integer systemId= (Integer)map.get("systemId");
		Integer langId= (Integer)map.get("langId");
		stringBuffer.append("select count(1) from t_arithmetic t1\n" +
			"inner join t_arithmetic_sysytem t2\n" +
			"on t1.systemId=t2.id\n" +
			"inner join t_arithmetic_lang t3\n" +
			"on t1.langId=t3.id \n" +
			"where 1=1 ");
		if(Strings.isNotBlank(name)){
			stringBuffer.append(" and t1.name like concat('%','"+name+"','%') ");
		}
		if(systemId!=null){
			stringBuffer.append(" and t1.systemId="+systemId+" ");
		}
		if(systemId!=null){
			stringBuffer.append(" and t1.langId="+langId+" ");
		}
		return  stringBuffer.toString();
	}

	//分页查询
	public String getPageArithmeticListSQL(Map<String,Object> map){
		StringBuffer stringBuffer=new StringBuffer();
		String name=(String)map.get("name");
		Integer systemId= (Integer)map.get("systemId");
		Integer langId= (Integer)map.get("langId");
		Integer pageNum= (Integer)map.get("pageNum");
		Integer pageSize= (Integer)map.get("pageSize");
		stringBuffer.append("select t1.*,t2.name as systemName,t3.name as langName from t_arithmetic t1\n" +
			"inner join t_arithmetic_sysytem t2\n" +
			"on t1.systemId=t2.id\n" +
			"inner join t_arithmetic_lang t3\n" +
			"on t1.langId=t3.id \n" +
			"where 1=1 ");
		if(Strings.isNotBlank(name)){
			stringBuffer.append(" and t1.name like concat('%','"+name+"','%') ");
		}
		if(systemId!=null){
			stringBuffer.append(" and t1.systemId="+systemId+" ");
		}
		if(systemId!=null){
			stringBuffer.append(" and t1.langId="+langId+" ");
		}
		stringBuffer.append("order by  t1.ctime desc limit "+(pageNum-1)*pageNum+","+pageSize+"");
		return  stringBuffer.toString();
	}

}


}
